import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import filesize from 'rollup-plugin-filesize';
import dts from 'rollup-plugin-dts';

// 通用插件配置
const commonPlugins = [
    typescript({
        tsconfig: './tsconfig.json',
        sourceMap: false,
        inlineSources: false
    }),
    nodeResolve()
];

// 生产环境插件（包含压缩）
const productionPlugins = [
    ...commonPlugins,
    terser({
        compress: {
            drop_console: false, // 保留 console.log
            drop_debugger: true,
            pure_funcs: ['console.debug'], // 移除 console.debug
        },
        mangle: {
            reserved: ['RandomName', 'IRandomName'] // 保留类名和接口名不被混淆
        },
        format: {
            comments: false, // 移除注释
        }
    }),
    filesize({
        showMinifiedSize: true,
        showGzippedSize: true
    })
];

const config = [
    // ES模块构建（压缩版）
    {
        input: 'index.ts',
        output: {
            file: 'dist/index.esm.min.js',
            format: 'es',
            sourcemap: false
        },
        plugins: productionPlugins
    },
    // CommonJS构建（压缩版）
    {
        input: 'index.ts',
        output: {
            file: 'dist/index.cjs.min.cjs',
            format: 'cjs',
            sourcemap: false,
            exports: 'named'
        },
        plugins: productionPlugins
    },
    // UMD构建（压缩版）
    {
        input: 'index.ts',
        output: {
            file: 'dist/index.umd.min.js',
            format: 'umd',
            name: 'RandomNameGenerator',
            sourcemap: false,
            exports: 'named'
        },
        plugins: productionPlugins
    },
    // TypeScript类型定义
    {
        input: 'index.ts',
        output: {
            file: 'dist/index.d.ts',
            format: 'es'
        },
        plugins: [dts()]
    }
];

export default config;
